package com.sgb.kjwl.viewmodel.manage;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.RegexUtils;
import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity;
import com.sgb.kjwl.repository.ManageRepertory;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author 张驰
 * @date 2021/6/4 10:43
 * @Description 个人网站编辑界面
 * @name com.sgb.kjwl.viewmodel.manage
 */
public class WebsiteUpdataMyXiXinModel extends BaseViewModel {

    private ManageRepertory repertory;
    private MutableLiveData<WebsiteInfoEntity> websiteInfoEntityLiveData = new MutableLiveData<>(new WebsiteInfoEntity());

    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity>>>> businessType = new MutableLiveData<>();


    public WebsiteUpdataMyXiXinModel(ManageRepertory repertory) {
        this.repertory = repertory;
    }

    public MutableLiveData<WebsiteInfoEntity> getWebsiteInfoData() {
        return websiteInfoEntityLiveData;
    }

    /**
     * 图片上传
     * @param mContext
     * @param file
     */
    public void uploadAvatar(Context mContext, File file) {
        DialogHelper.showProgressDialog(mContext, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        //创建map集合
        Map<String, RequestBody> photos = new HashMap<>();
        //将文件转换成多媒体文件，方便上传
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + RegexUtils.getReplaceAll(file.getName(), "[\u4e00-\u9fa5]", "") + "image.jpg\"", requestBody);
        //接口请求
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data == null || data.getData() == null) {
                    MToast.showToast(mContext, "上传失败");
                    return;
                }
                getMessage().postValue(data.getData().toString());
            }
        });
    }

    public void saveOrUpdate(Context activity, Map<String, Object> map) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().allSiteAddOrUpdate(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    //getMessage().postValue("操作成功");
                    getWebsiteInfoData().postValue(null);
                }
            }
        });
    }

    //经营类型
    public void getBusinessType() {
        APIManager.getInstance().getBusinessManagementAPI().getNewServiceType(0).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>> data) {
                if (data != null && data.getData() != null){
                    businessType.postValue(data.getData());
                }

            }
        });
    }
}
